package com.luzhikun.filterConfig;

import javax.servlet.*;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @Author Rookie Coder
 * @Description FilterConfig值的是Filter在web.xml中的注册信息
 * @Date
 **/
public class MyFilterConfig implements Filter {

    private FilterConfig filterConfig;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        // 获取Filter的Name
        String filterName = filterConfig.getFilterName();
        System.out.println("filterName------" + filterName);

        // 获取filter的初始化参数的值
        String namespace = filterConfig.getInitParameter("namespace");
        System.out.println(namespace);

        // 获取所有初始化参数的值
        Enumeration<String> initParameterNames = filterConfig.getInitParameterNames();
        while (initParameterNames.hasMoreElements()) {
            String s = initParameterNames.nextElement();
            String initParameter = filterConfig.getInitParameter(s);
            System.out.println(initParameter);
        }

        //获取全局域对象
        ServletContext servletContext = filterConfig.getServletContext();
        System.out.println(servletContext);
//        servletContext.setInitParameter("username","吴杰");
//        Object username = servletContext.getAttribute("username");
//        System.out.println(username);


    }

    @Override
    public void destroy() {

    }
}
